<template>
  <el-dialog :close-on-click-modal="false" :title="form.id ? '编辑' : '新增'" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" type="text" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="时段" prop="timeSegment">
        <el-time-picker is-range v-model="form.timeSegment" value-format="HH:mm:ss" format="HH:mm" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" @change="change"></el-time-picker>
        <!-- <selectTimeRanges v-model="form.timeSegment" @input="workTimeInfoChange"></selectTimeRanges> -->
      </el-form-item>
      <el-form-item label="时长" prop="timeHour">
        {{form.timeHour ? parseSeconds(form.timeHour) : '0分'}}
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { add, update } from "@/api/timeSegment";
import {parseSeconds} from '@/utils'
import selectTimeRanges from "@/views/attendWorkShift/components/selectTimeRanges.vue";
export default {
  components: { selectTimeRanges },
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    item: {
        type: Object,
        default: ()=>{}
    }
  },
  watch: {
    item: {
        handler(nv) {
            if(nv && nv.id) {
                this.form = nv;
                this.$set(this.form, 'timeSegment', [this.form.startTime, this.form.endTime])
            }
        },
        immediate: true
    }
  },
  data() {
    return {
      form: {},
      rules: {
        name: {required: true, message: '请输入名称', trigger: 'blur'},
        timeSegment: {required: true, message: '请选择时段', trigger: 'change'},
      },
    };
  },
  methods: {
    parseSeconds,
    close() {
      this.$emit("update:show", false);
    },
    submit() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          const res = this.form.id
            ? await update(this.form)
            : await add(this.form);
          if (res.code == 1) {
            this.$emit("confirm");
            this.close();
          }
        }
      });
    },
    change(e) {
        if(e) {
            const [_startTime, _endTime] = e;

            const startTime = this.formatTime(_startTime)
            const endTime = this.formatTime(_endTime)
            let today = this.parseTime(new Date(), '{y}-{m}-{d}')
            let startTemp = +new Date(today + " "+ startTime);
            let endTemp = +new Date(today + " "+ endTime);
            let second = endTemp - startTemp;

            this.$set(this.form, 'timeHour', parseInt(second / 1000))
            this.$set(this.form, 'startTime', startTime)
            this.$set(this.form, 'endTime', endTime)
        }
    },
    formatTime(time) {
      const [hours, minutes, seconds] = time.split(':').map(Number);
      const h = hours < 10 ? `0${hours}` : hours;
      const m = minutes < 10 ? `0${minutes}` : minutes;
      return `${h}:${m}:00`;
    }
  },
};
</script>